<template>
  <div>
    <van-icon @click="articleGood" :color="attitude===1?'red':'#777'" :name="attitude===1?'good-job':'good-job-o'" />
  </div>
</template>

<script>
import { addArticleGood, deleteArticleGood } from '@/api/artical.js'
export default {
  name: 'Good',
  data () {
    return {
      isGood: false
    }
  },
  props: {
    // 文章id
    articleId: {
      type: [Object, Number],
      required: true
    },
    // 用户对文章的态度, -1: 无态度，0-不喜欢，1-点赞
    attitude: {
      type: Number,
      required: true
    }
  },
  model: {
    prop: 'attitude'
  },
  methods: {
    async articleGood () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      try {
        if (this.attitude === 1) {
          await deleteArticleGood(this.articleId)
          this.$toast.success('取消点赞成功')
          this.$emit('input', -1)
        } else {
          await addArticleGood(this.articleId)
          this.$toast.success('点赞成功')
          this.$emit('input', 1)
        }
      } catch (err) {
        this.$toast('操作失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
.van-icon {
  font-size: 40px;
}
</style>
